#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：9.使用wait方法执行多个任务.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-11-07 19:49
'''
import asyncio
import time


async  def others(i):
    print("任务开始.....")
    await asyncio.sleep(2)
    print("任务结束....")
    return f"123{i}"

async def main():
    task_list = [loop.create_task(others(i)) for i in range(10)]
    done, pending = await asyncio.wait(task_list)
    """
        done：已完成任务
        pending：未完成任务
    """
    print("done结果：",done)
    print("pending结果：",pending)
    for res in pending:
        print(res.get_name())

    # wait返回的完成任务的顺序并不一定与传入的协程顺序一致，它并不保证顺序
    if done:
        for res in done:
            print(res.result())

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())